/**
 * 
 */
package org.rhq.plugins.tools.httpcheck;

import java.util.prefs.Preferences;

import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.ConfigurationUpdateStatus;
import org.rhq.core.domain.configuration.PropertySimple;
import org.rhq.core.domain.measurement.AvailabilityType;
import org.rhq.core.pluginapi.configuration.ConfigurationFacet;
import org.rhq.core.pluginapi.configuration.ConfigurationUpdateReport;
import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
import org.rhq.core.pluginapi.inventory.ResourceComponent;
import org.rhq.core.pluginapi.inventory.ResourceContext;

/**
 * ResourceComponent of the Http Server Checker.
 * 
 * In this resource, notification sender information will be configured here.
 * 
 * @author lgao
 *
 */
public class HttpCheckResourceComponent implements ResourceComponent,HttpCheckerConstants, ConfigurationFacet{

	/*
	 * Persists the data of the configuration in local Preferences.
	 * There is only one instance of this component, there is only one node in this Preferences.
	 */
	private Preferences store = Preferences.userNodeForPackage(HttpCheckResourceComponent.class);
	
	private ResourceContext context;
	
	public AvailabilityType getAvailability() {
		// always UP
		return AvailabilityType.UP;
	}

	public void start(ResourceContext _context)
			throws InvalidPluginConfigurationException, Exception {
		this.context = _context;
	}
	

	public void stop() {
		// noting to do here
		
	}
	
	private Preferences getStoreRootPref(){
		String rootPath = RES_CONFIG_GRP_KEY;
		return store.node(rootPath);
	}

	public Configuration loadResourceConfiguration() throws Exception {
		Configuration config = new Configuration();
		Preferences senderGrpPref = getStoreRootPref();
		
		// smtp port, default is 25, default ssl port is 465
		Integer defPort = Integer.valueOf(25);
		Integer smtpPort = senderGrpPref.getInt(RES_CONFIG_SMTP_PORT, defPort);
		PropertySimple smtpPortProp = new PropertySimple(RES_CONFIG_SMTP_PORT, smtpPort);
		config.put(smtpPortProp);
		
		// open smtp ??
		Boolean openSmtp = senderGrpPref.getBoolean(RES_CONFIG_OPEN_SMTP, false);
		PropertySimple openProp = new PropertySimple(RES_CONFIG_OPEN_SMTP, openSmtp);
		config.put(openProp);
		
		// smtp host
		String smtpHost = senderGrpPref.get(RES_CONFIG_SENDER_HOST, "");
		PropertySimple smtpHostProp = new PropertySimple(RES_CONFIG_SENDER_HOST, smtpHost);
		config.put(smtpHostProp);
		
		// mail from
		String currentSender = senderGrpPref.get(RES_CONFIG_SENDER_MAIL_FROM, "");
		PropertySimple senderProp = new PropertySimple(RES_CONFIG_SENDER_MAIL_FROM,currentSender);
		config.put(senderProp);

		// use auth ?
		Boolean useAuth = senderGrpPref.getBoolean(RES_CONFIG_SENDER_AUTH, false);
		PropertySimple authProp = new PropertySimple(RES_CONFIG_SENDER_AUTH, useAuth);
		config.put(authProp);
		
		// mail username
		String username = senderGrpPref.get(RES_CONFIG_SENDER_USER, "");
		PropertySimple userNameProp = new PropertySimple(RES_CONFIG_SENDER_USER, username);
		config.put(userNameProp);
		
		// mail password
		String senderPwd = senderGrpPref.get(RES_CONFIG_SENDER_PWD, "");
		PropertySimple senderPwdProp = new PropertySimple(RES_CONFIG_SENDER_PWD,senderPwd);
		config.put(senderPwdProp);
		
		return config;
	}

	public void updateResourceConfiguration(ConfigurationUpdateReport report) {
		Configuration config = report.getConfiguration();
		String senderMail = config.getSimpleValue(RES_CONFIG_SENDER_MAIL_FROM, null);
		String senderPwd = config.getSimpleValue(RES_CONFIG_SENDER_PWD, null);
		String smtpHost = config.getSimpleValue(RES_CONFIG_SENDER_HOST, null);
		Boolean useAuth = Boolean.valueOf(config.getSimpleValue(RES_CONFIG_SENDER_AUTH, "false"));
		String userName = config.getSimpleValue(RES_CONFIG_SENDER_USER, null);
		Boolean openSmtp = Boolean.valueOf(config.getSimpleValue(RES_CONFIG_OPEN_SMTP, "false"));
		
		// smtp port
		Integer smtpPort = Integer.valueOf(config.getSimpleValue(RES_CONFIG_SMTP_PORT, "25"));
		
		if(openSmtp){
			if(useAuth && (userName == null || senderPwd == null)){
				report.setStatus(ConfigurationUpdateStatus.FAILURE);
				throw new IllegalArgumentException("UserName and Password must be configured when use smtp auth");
			}
			if(null == senderMail || null == smtpHost){
				report.setStatus(ConfigurationUpdateStatus.FAILURE);
				throw new IllegalStateException("Wrong configuration.");
			}
		}
		
		Preferences senderGrpPref = getStoreRootPref();
		senderGrpPref.put(RES_CONFIG_SENDER_MAIL_FROM, senderMail);
		senderGrpPref.put(RES_CONFIG_SENDER_PWD, senderPwd);
		senderGrpPref.put(RES_CONFIG_SENDER_HOST, smtpHost);
		senderGrpPref.putBoolean(RES_CONFIG_SENDER_AUTH, useAuth);
		senderGrpPref.putBoolean(RES_CONFIG_OPEN_SMTP, openSmtp);
		senderGrpPref.put(RES_CONFIG_SENDER_USER, userName);
		senderGrpPref.putInt(RES_CONFIG_SMTP_PORT, smtpPort);
		
		report.setStatus(ConfigurationUpdateStatus.SUCCESS);
		return;		
	}

}